//
//  AppDelegate.m
//  优学荟管理系统
//
//  Created by 星道 on 17/6/26.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "AppDelegate.h"
#import "LoginVC.h"
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    application.statusBarHidden = NO;

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.rootViewController = [LoginVC new];

    [self.window makeKeyAndVisible];


    //极光推送配置
    self.SetupPushNotifications(launchOptions);

    // 全局键盘控制
    [self IQKeyboardManager];
    
    // 移除角标
    self.RemoveBadge ();
        
    return YES;
}


#pragma mark - 系统方法
/**
 回调方法
 */
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    //注册APNs成功并上报DeviceToken
    self.RegisterDeviceToken(deviceToken);
}

/**
 实现注册APNs失败接口
 */
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    UIAlertController *Alert = [UIAlertController alertControllerWithTitle:@"推送注册失败" message:@"非常抱歉!推送服务启动失败,您可以尝试再次启动APP重新注册该服务" preferredStyle:(UIAlertControllerStyleAlert)];
    [Alert addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:nil]];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:Alert animated:YES completion:nil];
}




/**
 全局键盘控制
 */
- (void)IQKeyboardManager{

    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
    //控制点击背景是否收起键盘
    manager.shouldResignOnTouchOutside = YES;
    //控制键盘上的工具条文字颜色是否用户自定义。
    manager.shouldToolbarUsesTextFieldTintColor = YES;
    //控制是否显示键盘上的工具条
    manager.enableAutoToolbar = NO;
    // 有多个输入框时，可以通过点击Toolbar 上的“前一个”“后一个”按钮来实现移动到不同的输入框
    manager.toolbarManageBehaviour = IQAutoToolbarByPosition;
    // 输入框距离键盘的距离
    manager.keyboardDistanceFromTextField = 10.0f;

}
//// 微信回调处理
//
//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
//
//    // 此处设置了代理,需要遵守协议: WXApiDelegate,并实现 - (void)onResp:(BaseResp *)resp; 方法
//
//
//    return [WXApi handleOpenURL:url delegate:self];
//}
//- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
//    // 此处设置了代理,需要遵守协议: WXApiDelegate 并实现 - (void)onResp:(BaseResp *)resp; 方法
//   // [WXApi handleOpenURL:url delegate:self];
//    if ([url.host isEqualToString:@"safepay"]) {
//        // 支付跳转支付宝钱包进行支付，处理支付结果
//        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
//            NSLog(@"result = %@",resultDic);
//
//
//        }];
//
//
//    }
//
//    return YES;
//}
//// NOTE: 9.0以后使用新API接口
//- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
//{
//
//
//
//
//    [WXApi handleOpenURL:url delegate:self];
//    if ([url.host isEqualToString:@"safepay"]) {
//        // 支付跳转支付宝钱包进行支付，处理支付结果
//        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
//            NSLog(@"result = %@",resultDic);
//
//        }];
//
//        return YES;
//    }
//
//
//
//
//    return YES;
//
//}


/*! @brief 发送一个sendReq后，收到微信的回应
 *
 * 收到一个来自微信的处理结果。调用一次sendReq后会收到onResp。
 * 可能收到的处理结果有SendMessageToWXResp、SendAuthResp等。
 * resp具体的回应内容，是自动释放的
 */
//-(void) onResp:(BaseResp*)resp{
//    //取消交易 0 -2 null  正常交易 0 0 null
//    NSLog(@"微信回调resp--%d %d %@",resp.type,resp.errCode,resp.errStr);
//
//    NSDictionary *dict = [NSDictionary dictionaryWithObject:@(resp.errCode) forKey:@"errCode"];
//
//    [[NSNotificationCenter defaultCenter] postNotificationName:WXCallbackNotification object:nil userInfo:dict];
//
//}

//获取当前view所在的控制器VC
+ (AppDelegate *)shareAppDelegate{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
-(UIViewController *)getCurrentVC{
    
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}

-(UIViewController *)getCurrentUIVC
{
    UIViewController  *superVC = [self getCurrentVC];
    
    
    if (superVC.presentedViewController) {
        
        return superVC.presentedViewController;
        
    }else if ([superVC isKindOfClass:[UITabBarController class]]) {
        
        UIViewController  *tabSelectVC = ((UITabBarController*)superVC).selectedViewController;
        
        if ([tabSelectVC isKindOfClass:[UINavigationController class]]) {
            
            return ((UINavigationController*)tabSelectVC).viewControllers.lastObject;
        }
        return tabSelectVC;
    }else
        if ([superVC isKindOfClass:[UINavigationController class]]) {
            
            return ((UINavigationController*)superVC).viewControllers.lastObject;
        }
    return superVC;
}






@end
